
<!DOCTYPE html>
<html lang="zh-CN" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>环境贴图技术 - 浪子之心</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Blog,"> 
    <meta name="description" content="MrChen Bolg,大纲
环境贴图
立方体贴图采样如上
采样缺陷当同一角度，不同位置看过去会得到同样采样，所以不适合平面采样，解决方案后续说。

123456789101112131415161718192021222,"> 
    <meta name="author" content="Jack Chen"> 
    <link rel="alternative" href="atom.xml" title="浪子之心" type="application/atom+xml"> 
    <link rel="icon" href="/blog/img/favicon.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/blog/css/diaspora.css">

<meta name="generator" content="Hexo 5.4.1"><link rel="stylesheet" href="/blog/css/prism.css" type="text/css"></head>

<body class="loading">
    <span id="config-title" style="display:none">浪子之心</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://mrchenlearnspace.github.io/blog"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">环境贴图技术</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">环境贴图技术</h1>
        <div class="stuff">
            <span>四月 01, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/blog/tags/Shader/" rel="tag">Shader</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="大纲"><a href="#大纲" class="headerlink" title="大纲"></a>大纲</h1><p><img src="https://img-blog.csdnimg.cn/88c6172ddf434fe08f2402d04f5c72ed.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAUEMgRXJyb3I=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center" alt="在这里插入图片描述"></p>
<h1 id="环境贴图"><a href="#环境贴图" class="headerlink" title="环境贴图"></a>环境贴图</h1><p><img src="https://img-blog.csdnimg.cn/3dbe7748467c4b17b46bef1315a907f5.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAUEMgRXJyb3I=,size_15,color_FFFFFF,t_70,g_se,x_16" alt="请添加图片描述"></p>
<p>立方体贴图采样如上</p>
<h2 id="采样缺陷"><a href="#采样缺陷" class="headerlink" title="采样缺陷"></a>采样缺陷</h2><p>当同一角度，不同位置看过去会得到同样采样，所以不适合平面采样，解决方案后续说。</p>
<p><img src="https://img-blog.csdnimg.cn/992f6d6e2a2544f08ae0c748c47a10b6.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAUEMgRXJyb3I=,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">v2f <span class="title">vert</span> <span class="params">(appdata v)</span></span></span><br><span class="line"><span class="function">         </span>&#123;</span><br><span class="line">             v2f o;</span><br><span class="line">             o.pos = <span class="built_in">UnityObjectToClipPos</span>(v.vertex);</span><br><span class="line">             o.normal_world = <span class="built_in">normalize</span>(<span class="built_in">mul</span>((v.normal),unity_WorldToObject).xyz);</span><br><span class="line">             o.tangent_world = <span class="built_in">normalize</span>(<span class="built_in">mul</span>((v.tangent),unity_WorldToObject).xyz);</span><br><span class="line">             o.binormal_world=<span class="built_in">normalize</span>(<span class="built_in">cross</span>(o.normal_world,o.tangent_world) * v.tangent.w);</span><br><span class="line">             o.pos_world = <span class="built_in">normalize</span>(<span class="built_in">mul</span>((v.vertex),unity_WorldToObject).xyz);</span><br><span class="line">             o.uv = v.uv*_NormalMap_ST.xy+_NormalMap_ST.zw;</span><br><span class="line">             </span><br><span class="line">             <span class="keyword">return</span> o;</span><br><span class="line">         &#125;</span><br><span class="line"></span><br><span class="line">         <span class="function">fixed4 <span class="title">frag</span> <span class="params">(v2f i)</span> : SV_Target</span></span><br><span class="line"><span class="function">         &#123;</span></span><br><span class="line">             <span class="comment">// sample the texture</span></span><br><span class="line">             <span class="comment">//fixed4 base_col = tex2D(_MainTex, i.uv);</span></span><br><span class="line">             fixed4 base_col = <span class="built_in">fixed4</span>(<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>);</span><br><span class="line">             half3 normaldata = <span class="built_in">UnpackNormal</span>( <span class="built_in">tex2D</span>(_NormalMap, i.uv));</span><br><span class="line">             half3 normal_world = <span class="built_in">normalize</span>(i.normal_world);</span><br><span class="line">             half3 tangent_world = <span class="built_in">normalize</span>(i.tangent_world);</span><br><span class="line">             half3 binormal_world = <span class="built_in">normalize</span>(i.binormal_world);</span><br><span class="line">             normal_world=<span class="built_in">mul</span>(normaldata, <span class="built_in">float3x3</span>(tangent_world,binormal_world,normal_world));</span><br><span class="line"></span><br><span class="line">             half3 view_dir = <span class="built_in">normalize</span>(_WorldSpaceCameraPos.xyz-i.pos_world.xyz);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">             half3 reflect_dir =<span class="built_in">reflect</span>(-view_dir,normal_world);</span><br><span class="line">             half4 finish_col = base_col;</span><br><span class="line">             </span><br><span class="line">             half4 Cube_col=<span class="built_in">texCUBE</span>(_CubeMap,reflect_dir); </span><br><span class="line">             half3 CubeHDR_col=<span class="built_in">DecodeHDR</span>(Cube_col,_CubeMap_HDR);</span><br><span class="line">             finish_col.rgb+=CubeHDR_col;</span><br><span class="line">             <span class="keyword">return</span> finish_col;</span><br><span class="line">         &#125;</span><br></pre></td></tr></table></figure>



<h1 id="IBL-基于图像的光照技术"><a href="#IBL-基于图像的光照技术" class="headerlink" title="IBL 基于图像的光照技术"></a>IBL 基于图像的光照技术</h1><h2 id="预计算卷积"><a href="#预计算卷积" class="headerlink" title="预计算卷积"></a>预计算卷积</h2><p>通过模糊，得到不同层次的mip Map，便于模拟相应的磨砂层次。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">half4 Cube_col=<span class="built_in">texCUBElod</span>(_CubeMap,<span class="built_in">float4</span>(reflect_dir,_Roughness))</span><br></pre></td></tr></table></figure>

<p>粗糙度变成非线性</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">roughness=roughness*(<span class="number">1.7</span><span class="number">-0.7</span>*roughness);</span><br></pre></td></tr></table></figure>

<h2 id="反射探针"><a href="#反射探针" class="headerlink" title="反射探针"></a>反射探针</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">half4 Cube_col = <span class="built_in">UNITY_SAMPLE_TEXCUBE_LOD</span>(unity_SpecCube0,reflect_dir,_Roughness);</span><br></pre></td></tr></table></figure>

<p>为了用反射探针</p>
<p>UNITY_SAMPLE_TEXCUBE_LOD(反射贴图，反射向量，mip)</p>
<p>UNITY_SAMPLE_TEXCUBE(反射贴图，反射向量)  </p>
<p>反射探针贴图</p>
<p>unity_SpecCube0 </p>
<h1 id="SH球谐光照"><a href="#SH球谐光照" class="headerlink" title="SH球谐光照"></a>SH球谐光照</h1><h2 id="造轮子"><a href="#造轮子" class="headerlink" title="造轮子"></a>造轮子</h2><h3 id="shader"><a href="#shader" class="headerlink" title="shader"></a>shader</h3><p>将光照信息记录下来，实现间接光照。</p>
<p>属性</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">custom_SHAr(<span class="string">&quot;Custom SHAr&quot;</span>, Vector) = (<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">custom_SHAg(<span class="string">&quot;Custom SHAg&quot;</span>, Vector) = (<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">custom_SHAb(<span class="string">&quot;Custom SHAb&quot;</span>, Vector) = (<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">custom_SHBr(<span class="string">&quot;Custom SHBr&quot;</span>, Vector) = (<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">custom_SHBg(<span class="string">&quot;Custom SHBg&quot;</span>, Vector) = (<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">custom_SHBb(<span class="string">&quot;Custom SHBb&quot;</span>, Vector) = (<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">custom_SHC(<span class="string">&quot;Custom SHC&quot;</span>, Vector) = (<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>)</span><br></pre></td></tr></table></figure>

<p>片元</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">float4 normalForSH = <span class="built_in">float4</span>(normal_dir, <span class="number">1.0</span>);</span><br><span class="line"><span class="comment">//SHEvalLinearL0L1</span></span><br><span class="line">half3 x;</span><br><span class="line">x.r = <span class="built_in">dot</span>(custom_SHAr, normalForSH);</span><br><span class="line">x.g = <span class="built_in">dot</span>(custom_SHAg, normalForSH);</span><br><span class="line">x.b = <span class="built_in">dot</span>(custom_SHAb, normalForSH);</span><br><span class="line"></span><br><span class="line"><span class="comment">//SHEvalLinearL2</span></span><br><span class="line">half3 x1, x2;</span><br><span class="line"><span class="comment">// 4 of the quadratic (L2) polynomials</span></span><br><span class="line">half4 vB = normalForSH.xyzz * normalForSH.yzzx;</span><br><span class="line">x1.r = <span class="built_in">dot</span>(custom_SHBr, vB);</span><br><span class="line">x1.g = <span class="built_in">dot</span>(custom_SHBg, vB);</span><br><span class="line">x1.b = <span class="built_in">dot</span>(custom_SHBb, vB);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Final (5th) quadratic (L2) polynomial</span></span><br><span class="line">half vC = normalForSH.x*normalForSH.x - normalForSH.y*normalForSH.y;</span><br><span class="line">x2 = custom_SHC.rgb * vC;</span><br><span class="line"></span><br><span class="line">float3 sh = <span class="built_in">max</span>(<span class="built_in">float3</span>(<span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">0.0</span>), (x + x1 + x2));</span><br><span class="line">sh = <span class="built_in">pow</span>(sh, <span class="number">1.0</span> / <span class="number">2.2</span>);</span><br><span class="line"></span><br><span class="line">half3 env_color = sh;</span><br></pre></td></tr></table></figure>

<p>部分函数可以放在顶点着色器中</p>
<h3 id="获取属性工具"><a href="#获取属性工具" class="headerlink" title="获取属性工具"></a>获取属性工具</h3><h4 id="SphericalHarmonicsCoefficient"><a href="#SphericalHarmonicsCoefficient" class="headerlink" title="SphericalHarmonicsCoefficient"></a>SphericalHarmonicsCoefficient</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 球谐光照因子计算方法,结果可以跟Unity内部的算法匹配上</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> http://sunandblackcat.com/tipFullView.php?l=eng&amp;topicid=32&amp;topic=Spherical-Harmonics-From-Cube-Texture</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> https://github.com/Microsoft/DirectXMath</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> http://www.ppsloan.org/publications/StupidSH36.pdf</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"><span class="keyword">using</span> UnityEditor;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class</span> <span class="title">SphericalHarmonicsCoefficient</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sphericalHarmonicsFromCubemap9</span>(<span class="params">Cubemap cubeTexture, <span class="keyword">ref</span> Vector3[] output</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// allocate memory for calculations</span></span><br><span class="line">        <span class="built_in">float</span>[] resultR = <span class="keyword">new</span> <span class="built_in">float</span>[<span class="number">9</span>];</span><br><span class="line">        <span class="built_in">float</span>[] resultG = <span class="keyword">new</span> <span class="built_in">float</span>[<span class="number">9</span>];</span><br><span class="line">        <span class="built_in">float</span>[] resultB = <span class="keyword">new</span> <span class="built_in">float</span>[<span class="number">9</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">// initialize values</span></span><br><span class="line">        <span class="built_in">float</span> fWt = <span class="number">0.0f</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">uint</span> i = <span class="number">0</span>; i &lt; <span class="number">9</span>; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            resultR[i] = <span class="number">0</span>;</span><br><span class="line">            resultG[i] = <span class="number">0</span>;</span><br><span class="line">            resultB[i] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">float</span>[] shBuff = <span class="keyword">new</span> <span class="built_in">float</span>[<span class="number">9</span>];</span><br><span class="line">        <span class="built_in">float</span>[] shBuffB = <span class="keyword">new</span> <span class="built_in">float</span>[<span class="number">9</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">// for each face of cube texture</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> face = <span class="number">0</span>; face &lt; <span class="number">6</span>; face++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// step between two texels for range [0, 1]</span></span><br><span class="line">            <span class="built_in">float</span> invWidth = <span class="number">1.0f</span> / cubeTexture.width;</span><br><span class="line">            <span class="comment">// initial negative bound for range [-1, 1]</span></span><br><span class="line">            <span class="built_in">float</span> negativeBound = <span class="number">-1.0f</span> + invWidth;</span><br><span class="line">            <span class="comment">// step between two texels for range [-1, 1]</span></span><br><span class="line">            <span class="built_in">float</span> invWidthBy2 = <span class="number">2.0f</span> / cubeTexture.width;</span><br><span class="line"></span><br><span class="line">            Color[] data = cubeTexture.GetPixels((CubemapFace)face);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> (<span class="built_in">int</span> y = <span class="number">0</span>; y &lt; cubeTexture.width; y++)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="comment">// texture coordinate V in range [-1 to 1]</span></span><br><span class="line">                <span class="built_in">float</span> fV = negativeBound + y * invWidthBy2;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">for</span> (<span class="built_in">int</span> x = <span class="number">0</span>; x &lt; cubeTexture.width; x++)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="comment">// texture coordinate U in range [-1 to 1]</span></span><br><span class="line">                    <span class="built_in">float</span> fU = negativeBound + x * invWidthBy2;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">// determine direction from center of cube texture to current texel</span></span><br><span class="line">                    Vector3 dir;</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">switch</span> ((CubemapFace)face)</span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="keyword">case</span> CubemapFace.PositiveX:</span><br><span class="line">                            dir.x = <span class="number">1.0f</span>;</span><br><span class="line">                            dir.y = <span class="number">1.0f</span> - (invWidthBy2 * y + invWidth);</span><br><span class="line">                            dir.z = <span class="number">1.0f</span> - (invWidthBy2 * x + invWidth);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> CubemapFace.NegativeX:</span><br><span class="line">                            dir.x = <span class="number">-1.0f</span>;</span><br><span class="line">                            dir.y = <span class="number">1.0f</span> - (invWidthBy2 * y + invWidth);</span><br><span class="line">                            dir.z = <span class="number">-1.0f</span> + (invWidthBy2 * x + invWidth);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> CubemapFace.PositiveY:</span><br><span class="line">                            dir.x = <span class="number">-1.0f</span> + (invWidthBy2 * x + invWidth);</span><br><span class="line">                            dir.y = <span class="number">1.0f</span>;</span><br><span class="line">                            dir.z = <span class="number">-1.0f</span> + (invWidthBy2 * y + invWidth);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> CubemapFace.NegativeY:</span><br><span class="line">                            dir.x = <span class="number">-1.0f</span> + (invWidthBy2 * x + invWidth);</span><br><span class="line">                            dir.y = <span class="number">-1.0f</span>;</span><br><span class="line">                            dir.z = <span class="number">1.0f</span> - (invWidthBy2 * y + invWidth);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> CubemapFace.PositiveZ:</span><br><span class="line">                            dir.x = <span class="number">-1.0f</span> + (invWidthBy2 * x + invWidth);</span><br><span class="line">                            dir.y = <span class="number">1.0f</span> - (invWidthBy2 * y + invWidth);</span><br><span class="line">                            dir.z = <span class="number">1.0f</span>;</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> CubemapFace.NegativeZ:</span><br><span class="line">                            dir.x = <span class="number">1.0f</span> - (invWidthBy2 * x + invWidth);</span><br><span class="line">                            dir.y = <span class="number">1.0f</span> - (invWidthBy2 * y + invWidth);</span><br><span class="line">                            dir.z = <span class="number">-1.0f</span>;</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="literal">default</span>:</span><br><span class="line">                            <span class="keyword">return</span>;</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">// normalize direction</span></span><br><span class="line">                    dir = dir.normalized;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">// scale factor depending on distance from center of the face</span></span><br><span class="line">                    <span class="built_in">float</span> fDiffSolid = <span class="number">4.0f</span> / ((<span class="number">1.0f</span> + fU * fU + fV * fV) * Mathf.Sqrt(<span class="number">1.0f</span> + fU * fU + fV * fV));</span><br><span class="line">                    fWt += fDiffSolid;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">// calculate coefficients of spherical harmonics for current direction</span></span><br><span class="line">                    sphericalHarmonicsEvaluateDirection9(<span class="keyword">ref</span> shBuff, dir);</span><br><span class="line">                    <span class="comment">//XMSHEvalDirection(dir, ref shBuff);</span></span><br><span class="line"></span><br><span class="line">                    <span class="comment">// index of texel in texture</span></span><br><span class="line">                    <span class="built_in">int</span> pixOffsetIndex = x + y * cubeTexture.width;</span><br><span class="line">                    <span class="comment">// get color from texture and map to range [0, 1]</span></span><br><span class="line">                    Vector3 clr= <span class="keyword">new</span> Vector3(data[pixOffsetIndex].r, data[pixOffsetIndex].g, data[pixOffsetIndex].b);</span><br><span class="line">                    <span class="comment">//if (data[pixOffsetIndex].a == 1)</span></span><br><span class="line">                    <span class="comment">//&#123;</span></span><br><span class="line">                    <span class="comment">//    clr = new Vector3(data[pixOffsetIndex].r, data[pixOffsetIndex].g, data[pixOffsetIndex].b);</span></span><br><span class="line">                    <span class="comment">//&#125;</span></span><br><span class="line">                    <span class="comment">//else</span></span><br><span class="line">                    <span class="comment">//&#123;</span></span><br><span class="line">                    <span class="comment">//    clr = DecodeHDR(data[pixOffsetIndex]);</span></span><br><span class="line">                    <span class="comment">//&#125;</span></span><br><span class="line">                    <span class="keyword">if</span> (PlayerSettings.colorSpace == ColorSpace.Gamma)</span><br><span class="line">                    &#123;</span><br><span class="line">                        clr.x = Mathf.GammaToLinearSpace(clr.x);</span><br><span class="line">                        clr.y = Mathf.GammaToLinearSpace(clr.y);</span><br><span class="line">                        clr.z = Mathf.GammaToLinearSpace(clr.z);</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="comment">// scale color and add to previously accumulated coefficients</span></span><br><span class="line">                    sphericalHarmonicsScale9(<span class="keyword">ref</span> shBuffB, shBuff, clr.x * fDiffSolid);</span><br><span class="line">                    sphericalHarmonicsAdd9(<span class="keyword">ref</span> resultR, resultR, shBuffB);</span><br><span class="line">                    sphericalHarmonicsScale9(<span class="keyword">ref</span> shBuffB, shBuff, clr.y * fDiffSolid);</span><br><span class="line">                    sphericalHarmonicsAdd9(<span class="keyword">ref</span> resultG, resultG, shBuffB);</span><br><span class="line">                    sphericalHarmonicsScale9(<span class="keyword">ref</span> shBuffB, shBuff, clr.z * fDiffSolid);</span><br><span class="line">                    sphericalHarmonicsAdd9(<span class="keyword">ref</span> resultB, resultB, shBuffB);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// final scale for coefficients</span></span><br><span class="line">        <span class="built_in">float</span> fNormProj = (<span class="number">4.0f</span> * Mathf.PI) / fWt;</span><br><span class="line">        sphericalHarmonicsScale9(<span class="keyword">ref</span> resultR, resultR, fNormProj);</span><br><span class="line">        sphericalHarmonicsScale9(<span class="keyword">ref</span> resultG, resultG, fNormProj);</span><br><span class="line">        sphericalHarmonicsScale9(<span class="keyword">ref</span> resultB, resultB, fNormProj);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// save result</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">uint</span> i = <span class="number">0</span>; i &lt; <span class="number">9</span>; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            output[i].x = resultR[i];</span><br><span class="line">            output[i].y = resultG[i];</span><br><span class="line">            output[i].z = resultB[i];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> Vector3 <span class="title">DecodeHDR</span>(<span class="params">Color clr</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Vector3(clr.r, clr.g, clr.b) * clr.a;<span class="comment">// * Mathf.Pow(clr.a, 2);// * (Mathf.Pow(clr.a, 0.1f) * 1);</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sphericalHarmonicsEvaluateDirection9</span>(<span class="params"><span class="keyword">ref</span> <span class="built_in">float</span>[] outsh, Vector3 dir</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 86 clocks</span></span><br><span class="line">        <span class="comment">// Make sure all constants are never computed at runtime</span></span><br><span class="line">        <span class="keyword">const</span> <span class="built_in">float</span> kInv2SqrtPI = <span class="number">0.28209479177387814347403972578039f</span>; <span class="comment">// 1 / (2*sqrt(kPI))</span></span><br><span class="line">        <span class="keyword">const</span> <span class="built_in">float</span> kSqrt3Div2SqrtPI = <span class="number">0.48860251190291992158638462283835f</span>; <span class="comment">// sqrt(3) / (2*sqrt(kPI))</span></span><br><span class="line">        <span class="keyword">const</span> <span class="built_in">float</span> kSqrt15Div2SqrtPI = <span class="number">1.0925484305920790705433857058027f</span>; <span class="comment">// sqrt(15) / (2*sqrt(kPI))</span></span><br><span class="line">        <span class="keyword">const</span> <span class="built_in">float</span> k3Sqrt5Div4SqrtPI = <span class="number">0.94617469575756001809268107088713f</span>; <span class="comment">// 3 * sqrtf(5) / (4*sqrt(kPI))</span></span><br><span class="line">        <span class="keyword">const</span> <span class="built_in">float</span> kSqrt15Div4SqrtPI = <span class="number">0.54627421529603953527169285290135f</span>; <span class="comment">// sqrt(15) / (4*sqrt(kPI))</span></span><br><span class="line">        <span class="keyword">const</span> <span class="built_in">float</span> kOneThird = <span class="number">0.3333333333333333333333f</span>; <span class="comment">// 1.0/3.0</span></span><br><span class="line">        outsh[<span class="number">0</span>] = kInv2SqrtPI;</span><br><span class="line">        outsh[<span class="number">1</span>] = -dir.y * kSqrt3Div2SqrtPI;</span><br><span class="line">        outsh[<span class="number">2</span>] = dir.z * kSqrt3Div2SqrtPI;</span><br><span class="line">        outsh[<span class="number">3</span>] = -dir.x * kSqrt3Div2SqrtPI;</span><br><span class="line">        outsh[<span class="number">4</span>] = dir.x * dir.y * kSqrt15Div2SqrtPI;</span><br><span class="line">        outsh[<span class="number">5</span>] = -dir.y * dir.z * kSqrt15Div2SqrtPI;</span><br><span class="line">        outsh[<span class="number">6</span>] = (dir.z * dir.z - kOneThird) * k3Sqrt5Div4SqrtPI;</span><br><span class="line">        outsh[<span class="number">7</span>] = -dir.x * dir.z * kSqrt15Div2SqrtPI;</span><br><span class="line">        outsh[<span class="number">8</span>] = (dir.x * dir.x - dir.y * dir.y) * kSqrt15Div4SqrtPI;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sphericalHarmonicsAdd9</span>(<span class="params"><span class="keyword">ref</span> <span class="built_in">float</span>[] result, <span class="built_in">float</span>[] inputA, <span class="built_in">float</span>[] inputB</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; <span class="number">9</span>; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            result[i] = inputA[i] + inputB[i];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sphericalHarmonicsScale9</span>(<span class="params"><span class="keyword">ref</span> <span class="built_in">float</span>[] result, <span class="built_in">float</span>[] input, <span class="built_in">float</span> scale</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; <span class="number">9</span>; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            result[i] = input[i] * scale;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">readonly</span> <span class="built_in">float</span> s_fSqrtPI = Mathf.Sqrt(Mathf.PI);</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">readonly</span> <span class="built_in">float</span> fC0 = <span class="number">1.0f</span> / (<span class="number">2.0f</span> * s_fSqrtPI);</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">readonly</span> <span class="built_in">float</span> fC1 = Mathf.Sqrt(<span class="number">3.0f</span>) / (<span class="number">3.0f</span> * s_fSqrtPI);</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">readonly</span> <span class="built_in">float</span> fC2 = Mathf.Sqrt(<span class="number">15.0f</span>) / (<span class="number">8.0f</span> * s_fSqrtPI);</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">readonly</span> <span class="built_in">float</span> fC3 = Mathf.Sqrt(<span class="number">5.0f</span>) / (<span class="number">16.0f</span> * s_fSqrtPI);</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">readonly</span> <span class="built_in">float</span> fC4 = <span class="number">0.5f</span> * fC2;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">ConvertSHConstants</span>(<span class="params">Vector3[] sh, <span class="keyword">ref</span> Vector4[] SHArBrC</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">int</span> iC;</span><br><span class="line">        <span class="keyword">for</span> (iC = <span class="number">0</span>; iC &lt; <span class="number">3</span>; iC++)</span><br><span class="line">        &#123;</span><br><span class="line">            SHArBrC[iC].x = -fC1 * sh[<span class="number">3</span>][iC];</span><br><span class="line">            SHArBrC[iC].y = -fC1 * sh[<span class="number">1</span>][iC];</span><br><span class="line">            SHArBrC[iC].z = fC1 * sh[<span class="number">2</span>][iC];</span><br><span class="line">            SHArBrC[iC].w = fC0 * sh[<span class="number">0</span>][iC] - fC3 * sh[<span class="number">6</span>][iC];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (iC = <span class="number">0</span>; iC &lt; <span class="number">3</span>; iC++)</span><br><span class="line">        &#123;</span><br><span class="line">            SHArBrC[iC + <span class="number">3</span>].x = fC2 * sh[<span class="number">4</span>][iC];</span><br><span class="line">            SHArBrC[iC + <span class="number">3</span>].y = -fC2 * sh[<span class="number">5</span>][iC];</span><br><span class="line">            SHArBrC[iC + <span class="number">3</span>].z = <span class="number">3.0f</span> * fC3 * sh[<span class="number">6</span>][iC];</span><br><span class="line">            SHArBrC[iC + <span class="number">3</span>].w = -fC2 * sh[<span class="number">7</span>][iC];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        SHArBrC[<span class="number">6</span>].x = fC4 * sh[<span class="number">8</span>][<span class="number">0</span>];</span><br><span class="line">        SHArBrC[<span class="number">6</span>].y = fC4 * sh[<span class="number">8</span>][<span class="number">1</span>];</span><br><span class="line">        SHArBrC[<span class="number">6</span>].z = fC4 * sh[<span class="number">8</span>][<span class="number">2</span>];</span><br><span class="line">        SHArBrC[<span class="number">6</span>].w = <span class="number">1.0f</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="CubemapSHProjector"><a href="#CubemapSHProjector" class="headerlink" title="CubemapSHProjector"></a>CubemapSHProjector</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"><span class="keyword">using</span> UnityEditor;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">CubemapSHProjector</span> : <span class="title">EditorWindow</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//PUBLIC FIELDS</span></span><br><span class="line">    <span class="keyword">public</span> Texture envMap;</span><br><span class="line">    <span class="keyword">public</span> Transform go;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//PRIVATE FIELDS</span></span><br><span class="line">    <span class="keyword">private</span> Material    view_mat;</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">float</span>       view_mode;</span><br><span class="line">    <span class="keyword">private</span> Vector4[]   coefficients;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> SerializedObject so;</span><br><span class="line">    <span class="keyword">private</span> SerializedProperty sp_input_cubemap;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Texture2D tmp = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">CheckAndConvertEnvMap</span>(<span class="params"><span class="keyword">ref</span> Texture envMap, <span class="keyword">ref</span> Vector4[] sh_out</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (!envMap) <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">string</span> map_path = AssetDatabase.GetAssetPath(envMap);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">string</span>.IsNullOrEmpty(map_path)) <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">        TextureImporter ti = AssetImporter.GetAtPath(map_path) <span class="keyword">as</span> TextureImporter;</span><br><span class="line">        <span class="keyword">if</span> (!ti) <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">bool</span> read_able = ti.isReadable;</span><br><span class="line">        <span class="built_in">bool</span> need_reimport = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (ti.textureShape != TextureImporterShape.TextureCube)</span><br><span class="line">        &#123;</span><br><span class="line">            ti.textureShape = TextureImporterShape.TextureCube;</span><br><span class="line">            need_reimport = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!ti.mipmapEnabled)</span><br><span class="line">        &#123;</span><br><span class="line">            ti.mipmapEnabled = <span class="literal">true</span>;</span><br><span class="line">            need_reimport = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!ti.sRGBTexture)</span><br><span class="line">        &#123;</span><br><span class="line">            ti.sRGBTexture = <span class="literal">true</span>;</span><br><span class="line">            need_reimport = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (ti.filterMode != FilterMode.Trilinear)</span><br><span class="line">        &#123;</span><br><span class="line">            ti.filterMode = FilterMode.Trilinear;</span><br><span class="line">            need_reimport = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        TextureImporterSettings tis = <span class="keyword">new</span> TextureImporterSettings();</span><br><span class="line">        ti.ReadTextureSettings(tis);</span><br><span class="line">        <span class="keyword">if</span> (tis.cubemapConvolution != TextureImporterCubemapConvolution.Specular)</span><br><span class="line">        &#123;</span><br><span class="line">            tis.cubemapConvolution = TextureImporterCubemapConvolution.Specular;</span><br><span class="line">            ti.SetTextureSettings(tis);</span><br><span class="line">            need_reimport = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//if (ti.GetDefaultPlatformTextureSettings().maxTextureSize != 128)</span></span><br><span class="line">        <span class="comment">//&#123;</span></span><br><span class="line">        <span class="comment">//    TextureImporterPlatformSettings tips = new TextureImporterPlatformSettings();</span></span><br><span class="line">        <span class="comment">//    tips.maxTextureSize = 128;</span></span><br><span class="line">        <span class="comment">//    ti.SetPlatformTextureSettings(tips);</span></span><br><span class="line">        <span class="comment">//    ti.maxTextureSize = 128;</span></span><br><span class="line">        <span class="comment">//    need_reimport = true;</span></span><br><span class="line">        <span class="comment">//&#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!read_able)</span><br><span class="line">        &#123;</span><br><span class="line">            ti.isReadable = <span class="literal">true</span>;</span><br><span class="line">            need_reimport = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (need_reimport)</span><br><span class="line">        &#123;</span><br><span class="line">            ti.SaveAndReimport();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        envMap = AssetDatabase.LoadAssetAtPath&lt;Texture&gt;(map_path);</span><br><span class="line">        <span class="keyword">if</span> (!envMap) <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">        Vector3[] sh = <span class="keyword">new</span> Vector3[<span class="number">9</span>];</span><br><span class="line">        SphericalHarmonicsCoefficient.sphericalHarmonicsFromCubemap9((Cubemap)envMap, <span class="keyword">ref</span> sh);</span><br><span class="line">        SphericalHarmonicsCoefficient.ConvertSHConstants(sh, <span class="keyword">ref</span> sh_out);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (ti.isReadable != read_able)</span><br><span class="line">        &#123;</span><br><span class="line">            ti.isReadable = read_able;</span><br><span class="line">            ti.SaveAndReimport();</span><br><span class="line">            envMap = AssetDatabase.LoadAssetAtPath&lt;Texture&gt;(map_path);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    [<span class="meta">MenuItem(<span class="string">&quot;美术/SH系数生成&quot;</span>, false, 2100)</span>]</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Init</span>()</span></span><br><span class="line">    &#123;</span><br><span class="line">        CubemapSHProjector window = (CubemapSHProjector)EditorWindow.GetWindow(<span class="keyword">typeof</span>(CubemapSHProjector));</span><br><span class="line">        window.Show();</span><br><span class="line">        window.titleContent = <span class="keyword">new</span> GUIContent(<span class="string">&quot;SH生成器&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">OnFocus</span>()</span></span><br><span class="line">    &#123;</span><br><span class="line">        Initialize();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">OnEnable</span>()</span></span><br><span class="line">    &#123;</span><br><span class="line">        Initialize();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">Initialize</span>()</span></span><br><span class="line">    &#123;</span><br><span class="line">        so = <span class="keyword">new</span> SerializedObject(<span class="keyword">this</span>);</span><br><span class="line">        sp_input_cubemap = so.FindProperty(<span class="string">&quot;input_cubemap&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">OnGUI</span>()</span></span><br><span class="line">    &#123;</span><br><span class="line">        EditorGUI.BeginChangeCheck();</span><br><span class="line">        envMap = EditorGUILayout.ObjectField(<span class="string">&quot;环境图&quot;</span>, envMap, <span class="keyword">typeof</span>(Texture), <span class="literal">false</span>) <span class="keyword">as</span> Texture;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (envMap != <span class="literal">null</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            EditorGUILayout.Space();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (GUILayout.Button(<span class="string">&quot;Calc&quot;</span>))</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (envMap != <span class="literal">null</span>)</span><br><span class="line">                &#123;</span><br><span class="line">                    coefficients = <span class="keyword">new</span> Vector4[<span class="number">7</span>];</span><br><span class="line">                    CheckAndConvertEnvMap(<span class="keyword">ref</span> envMap, <span class="keyword">ref</span> coefficients);</span><br><span class="line">                &#125;</span><br><span class="line">                SceneView.RepaintAll();</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            EditorGUILayout.Space();</span><br><span class="line"></span><br><span class="line">            go = EditorGUILayout.ObjectField(<span class="string">&quot;Obj&quot;</span>, go, <span class="keyword">typeof</span>(Transform), <span class="literal">true</span>) <span class="keyword">as</span> Transform;</span><br><span class="line">            <span class="keyword">if</span> (go != <span class="literal">null</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">if</span> (GUILayout.Button(<span class="string">&quot;Apply&quot;</span>))</span><br><span class="line">                &#123;</span><br><span class="line">                    List&lt;Material&gt; mat_list = <span class="keyword">new</span> List&lt;Material&gt;();</span><br><span class="line">                    <span class="keyword">var</span> renders = go.GetComponentsInChildren&lt;Renderer&gt;();</span><br><span class="line">                    <span class="keyword">foreach</span> (<span class="keyword">var</span> render <span class="keyword">in</span> renders)</span><br><span class="line">                    &#123;</span><br><span class="line">                        mat_list.AddRange(render.sharedMaterials);</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">foreach</span> (<span class="keyword">var</span> mat <span class="keyword">in</span> mat_list)</span><br><span class="line">                    &#123;</span><br><span class="line">                        mat.SetVector(<span class="string">&quot;custom_SHAr&quot;</span>, coefficients[<span class="number">0</span>]);</span><br><span class="line">                        mat.SetVector(<span class="string">&quot;custom_SHAg&quot;</span>, coefficients[<span class="number">1</span>]);</span><br><span class="line">                        mat.SetVector(<span class="string">&quot;custom_SHAb&quot;</span>, coefficients[<span class="number">2</span>]);</span><br><span class="line">                        mat.SetVector(<span class="string">&quot;custom_SHBr&quot;</span>, coefficients[<span class="number">3</span>]);</span><br><span class="line">                        mat.SetVector(<span class="string">&quot;custom_SHBg&quot;</span>, coefficients[<span class="number">4</span>]);</span><br><span class="line">                        mat.SetVector(<span class="string">&quot;custom_SHBb&quot;</span>, coefficients[<span class="number">5</span>]);</span><br><span class="line">                        mat.SetVector(<span class="string">&quot;custom_SHC&quot;</span>, coefficients[<span class="number">6</span>]);</span><br><span class="line">                    &#125;</span><br><span class="line">                    mat_list.Clear();</span><br><span class="line">                    SceneView.RepaintAll();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            EditorGUILayout.Space();</span><br><span class="line"></span><br><span class="line">            <span class="comment">//print the 9 coefficients</span></span><br><span class="line">            <span class="keyword">if</span> (coefficients != <span class="literal">null</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                EditorGUILayout.LabelField(<span class="string">&quot;custom_SHAr&quot;</span> + <span class="string">&quot;: &quot;</span> + coefficients[<span class="number">0</span>].ToString(<span class="string">&quot;F4&quot;</span>));</span><br><span class="line">                EditorGUILayout.LabelField(<span class="string">&quot;custom_SHAg&quot;</span> + <span class="string">&quot;: &quot;</span> + coefficients[<span class="number">1</span>].ToString(<span class="string">&quot;F4&quot;</span>));</span><br><span class="line">                EditorGUILayout.LabelField(<span class="string">&quot;custom_SHAb&quot;</span> + <span class="string">&quot;: &quot;</span> + coefficients[<span class="number">2</span>].ToString(<span class="string">&quot;F4&quot;</span>));</span><br><span class="line">                EditorGUILayout.LabelField(<span class="string">&quot;custom_SHBr&quot;</span> + <span class="string">&quot;: &quot;</span> + coefficients[<span class="number">3</span>].ToString(<span class="string">&quot;F4&quot;</span>));</span><br><span class="line">                EditorGUILayout.LabelField(<span class="string">&quot;custom_SHBg&quot;</span> + <span class="string">&quot;: &quot;</span> + coefficients[<span class="number">4</span>].ToString(<span class="string">&quot;F4&quot;</span>));</span><br><span class="line">                EditorGUILayout.LabelField(<span class="string">&quot;custom_SHBb&quot;</span> + <span class="string">&quot;: &quot;</span> + coefficients[<span class="number">5</span>].ToString(<span class="string">&quot;F4&quot;</span>));</span><br><span class="line">                EditorGUILayout.LabelField(<span class="string">&quot;custom_SHC&quot;</span> + <span class="string">&quot;: &quot;</span> + coefficients[<span class="number">6</span>].ToString(<span class="string">&quot;F4&quot;</span>));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        EditorGUILayout.Space();</span><br><span class="line">        <span class="keyword">if</span> (tmp != <span class="literal">null</span>)</span><br><span class="line">            GUILayout.Label(tmp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Unity内置函数"><a href="#Unity内置函数" class="headerlink" title="Unity内置函数"></a>Unity内置函数</h2><p>换上小皮肤</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Tags&#123;<span class="string">&quot;LightMode&quot;</span>=<span class="string">&quot;ForwardBase&quot;</span>&#125;</span><br><span class="line">CGPROGRAM</span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> vertex vert</span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> fragment frag</span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> multi_compile_fwdbase</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;AutoLight.cginc&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;UnityCG.cginc&quot;</span></span></span><br></pre></td></tr></table></figure>

<p>片元</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">half3 env_color = <span class="built_in">ShadeSH9</span>(<span class="built_in">float4</span>(normal_dir,<span class="number">1.0</span>));</span><br></pre></td></tr></table></figure>

<p>完成</p>
<h2 id="光照探测器"><a href="#光照探测器" class="headerlink" title="光照探测器"></a>光照探测器</h2><p>光照探测器的小球记录下问题的球谐数值，然后物体在范围内时，用物体周围小球的球谐数值做插值。</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="/music/jucilang.mp3">
            </audio>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci='99f95a936c6fcdb1bd67'
        data-cs='c2d1bba3d6b7f9ade4f3de09a63925bd837c25ea'
        data-r='mrchenlearnspace.github.io'
        data-o='MrChenLearnSpace'
        data-a='MrChenLearnSpace'
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A4%A7%E7%BA%B2"><span class="toc-number">1.</span> <span class="toc-text">大纲</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E8%B4%B4%E5%9B%BE"><span class="toc-number">2.</span> <span class="toc-text">环境贴图</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%87%87%E6%A0%B7%E7%BC%BA%E9%99%B7"><span class="toc-number">2.1.</span> <span class="toc-text">采样缺陷</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#IBL-%E5%9F%BA%E4%BA%8E%E5%9B%BE%E5%83%8F%E7%9A%84%E5%85%89%E7%85%A7%E6%8A%80%E6%9C%AF"><span class="toc-number">3.</span> <span class="toc-text">IBL 基于图像的光照技术</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A2%84%E8%AE%A1%E7%AE%97%E5%8D%B7%E7%A7%AF"><span class="toc-number">3.1.</span> <span class="toc-text">预计算卷积</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%8D%E5%B0%84%E6%8E%A2%E9%92%88"><span class="toc-number">3.2.</span> <span class="toc-text">反射探针</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#SH%E7%90%83%E8%B0%90%E5%85%89%E7%85%A7"><span class="toc-number">4.</span> <span class="toc-text">SH球谐光照</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%A0%E8%BD%AE%E5%AD%90"><span class="toc-number">4.1.</span> <span class="toc-text">造轮子</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#shader"><span class="toc-number">4.1.1.</span> <span class="toc-text">shader</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E5%B1%9E%E6%80%A7%E5%B7%A5%E5%85%B7"><span class="toc-number">4.1.2.</span> <span class="toc-text">获取属性工具</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#SphericalHarmonicsCoefficient"><span class="toc-number">4.1.2.1.</span> <span class="toc-text">SphericalHarmonicsCoefficient</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#CubemapSHProjector"><span class="toc-number">4.1.2.2.</span> <span class="toc-text">CubemapSHProjector</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Unity%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0"><span class="toc-number">4.2.</span> <span class="toc-text">Unity内置函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%89%E7%85%A7%E6%8E%A2%E6%B5%8B%E5%99%A8"><span class="toc-number">4.3.</span> <span class="toc-text">光照探测器</span></a></li></ol></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/blog/js/plugin.js"></script>
<script src="/blog/js/typed.js"></script>
<script src="/blog/js/diaspora.js"></script>


<link rel="stylesheet" href="/blog/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/blog/photoswipe/default-skin/default-skin.css">


<script src="/blog/photoswipe/photoswipe.min.js"></script>
<script src="/blog/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



<script type="text/x-mathjax-config">
    MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
        tex2jax: { inlineMath: [ ["$", "$"], ["\\(","\\)"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno",skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']},
        TeX: {  noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
        messageStyle: "none"
    });
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>



<!-- Google Analytics -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YMJ8CBH8F7"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-YMJ8CBH8F7');
</script>
<!-- End Google Analytics -->


</html>
